from pptx.util import Inches, Pt
from pptx.enum.text import PP_ALIGN, MSO_VERTICAL_ANCHOR
from pptx.dml.color import RGBColor
from pptx.util import Pt
from io import BytesIO
from PIL import Image
from pptx.enum.shapes import MSO_SHAPE
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN
from pptx.enum.text import MSO_VERTICAL_ANCHOR


# 设置表格中某一行的字体大小和颜色
def set_table_row_font_size(table, row_index, font_size_pt,font_color_rgb=(0, 0, 0)):
    try:
        row = table.rows[row_index]
    except IndexError:
        raise ValueError(f"行索引 {row_index} 超出表格范围")

    for cell in row.cells:
        for paragraph in cell.text_frame.paragraphs:
            # 如果没有 run，添加一个
            if not paragraph.runs:
                paragraph.add_run()
            for run in paragraph.runs:
                run.font.size = Pt(font_size_pt)
                run.font.color.rgb = RGBColor(*font_color_rgb)
def set_cell_font_size(cell, font_size_pt):
    for paragraph in cell.text_frame.paragraphs:
        if not paragraph.runs:
            paragraph.add_run()  # 确保有 run
        for run in paragraph.runs:
            run.font.size = Pt(font_size_pt)

def add_text_box(slide, text, left, top, width, height,front_site):
    textbox = slide.shapes.add_textbox(Inches(left), Inches(top),width, height)
    text_frame = textbox.text_frame
    p = text_frame.paragraphs[0]
    p.text = text
    p.alignment = PP_ALIGN.CENTER
    run = p.runs[0]
    run.font.size = Pt(front_site)
    run.font.bold = True
    run.font.color.rgb = RGBColor(128, 128, 128)  # 黑色字体

def get_slide_height(slide):
    return slide.part.package.presentation_part.presentation.slide_height/914400

def get_slide_width(slide):
    return slide.part.package.presentation_part.presentation.slide_width/914400

#旋转图片
def rotate_image(jVal, angle):
    jVal.seek(0)
    img = Image.open(jVal)
    img = img.rotate(angle, expand=True)
    jVal = BytesIO()
    img.save(jVal, format='JPEG')  # 或 'PNG'，看你原图格式
    jVal.seek(0)
    return jVal

#对slide中的所有文本框设置字体大小
def set_all_textbox_font_size(slide, font_size_pt):
    for shape in slide.shapes:
        if not shape.has_text_frame:
            continue  # 不是文本框跳过

        text_frame = shape.text_frame
        for paragraph in text_frame.paragraphs:
            for run in paragraph.runs:
                run.font.size = Pt(font_size_pt)

def add_text_box(slide,left,top,label_width,label_height,text,font_size,text_line=False):
    label_height = Inches(font_size/72)
    label_box = slide.shapes.add_textbox(
            left,  # 靠左侧
            top,  # 垂直居中
            label_width,
            label_height
        )
    label_tf = label_box.text_frame
    #显示F+行数
    label_tf.text = text
    label_tf.paragraphs[0].font.size = Pt(font_size)
    if text_line:
        line = slide.shapes.add_shape(
            MSO_SHAPE.RECTANGLE,
            left,
            top+Inches(font_size/72+0.08),
            label_width,
            Pt(1)  # 线条高度
        )
        line.fill.solid()
        line.fill.fore_color.rgb = RGBColor(0, 0, 0)  # 黑色
        line.line.fill.background()  # 移除边框线

# ✅ 设置表格背景颜色
def set_table_background(table, color=RGBColor(255, 255, 255)):
    for row in table.rows:
        for cell in row.cells:
            cell.fill.solid()
            cell.fill.fore_color.rgb = color
# ✅ 给整张表格所有单元格加边框
def set_table_border(table, color="000000", width=Pt(0.5)):
    for row in table.rows:
        for cell in row.cells:
            set_cell_border(cell, color=color, width=width)

# ✅ 给指定行加边框
def set_table_row_border(table, row_index, color="B4B4B4", width=Pt(0.5)):
    for cell in table.rows[row_index].cells:
        set_cell_border(cell, color=color, width=width)

# ✅ 给指定列加边框
def set_table_col_border(table, col_index, color="B4B4B4", width=Pt(0.5)):
    for row in table.rows:
        set_cell_border(row.cells[col_index], color=color, width=width)

# ✅ 给表格字体居中
def set_cell_text_center(cell):
    # 设置段落水平居中
    cell.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLE
    paragraph = cell.text_frame.paragraphs[0]
    paragraph.alignment = PP_ALIGN.CENTER

# ✅ 设置边框
def set_cell_border(cell, color="B4B4B4", width=Pt(0.5)):
    from pptx.oxml import parse_xml
    from pptx.oxml.ns import nsdecls

    cell_xml = cell._tc
    tcPr = cell_xml.get_or_add_tcPr()

    border_xml = f"""
        <a:tcBorders {nsdecls('a')}>
            <a:lnL w="{width}">
                <a:solidFill><a:srgbClr val="{color}"/></a:solidFill>
                <a:prstDash val="solid"/>
            </a:lnL>
            <a:lnR w="{width}">
                <a:solidFill><a:srgbClr val="{color}"/></a:solidFill>
                <a:prstDash val="solid"/>
            </a:lnR>
            <a:lnT w="{width}">
                <a:solidFill><a:srgbClr val="{color}"/></a:solidFill>
                <a:prstDash val="solid"/>
            </a:lnT>
            <a:lnB w="{width}">
                <a:solidFill><a:srgbClr val="{color}"/></a:solidFill>
                <a:prstDash val="solid"/>
            </a:lnB>
        </a:tcBorders>
    """

    # 解析整个 tcBorders 节点，再一一添加子元素
    tcBorders = parse_xml(border_xml)
    for border in tcBorders:
        tcPr.append(border)
